package com.diary.database;

import com.diary.DBUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class login {

    public static boolean in(String user, String password) {
        // 返回一个布尔类型，如果至少有一条记录，则说明登陆成功

        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;

        try {
            connection = DBUtils.getConnection();
            String sql ="select * from users where user = ? and password = ? and live = 1";

            preparedStatement = connection.prepareStatement(sql);

            preparedStatement.setString(1,user);
            preparedStatement.setString(2,password);

            resultSet = preparedStatement.executeQuery();

//            System.out.println(resultSet);

            return resultSet.next();

        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
        finally {
            DBUtils.close(resultSet,preparedStatement, connection);

        }






//
//        Connection connection = null;
//        PreparedStatement preparedStatement = null;
//        ResultSet resultSet = null;
//
//        try {
//            //获取连接
//            connection = DBUtils.getConnection();
//            String sql = "select * from users where user == ? and password == ? and live == 1";
//
//            preparedStatement = connection.prepareStatement(sql);
//
//            preparedStatement.setString(1,"root");       //百分号表示通配符
//            preparedStatement.setString(2,"12123");
//            resultSet = preparedStatement.executeQuery();
//
//            while (resultSet.next()){
//                String c1 = resultSet.getString("c1");
//                System.out.println(c1);
//            }
//
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }
//        finally {
//            DBUtils.close(resultSet,preparedStatement,connection);
//        }






    }
}
